package graphics.rasterizing;

import graphics.SimpleModel;
import graphics.rasterizing.camera.CameraPosition;
import graphics.rasterizing.model.Model;
import graphics.rasterizing.model.Triangle;
import graphics.rasterizing.projection.OrhtogonalProjection;
import graphics.rasterizing.projection.PerspectiveProjection;
import graphics.rasterizing.shading.RandomColorShader;

import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JFrame;

import org.junit.Test;

import drawing.CgPanel;
import static util.MatrixOperationsF.*;
public class _RasterizerTest {
	@Test
	public void test() {
		final Collection<Triangle> tries = genTriangles();
		float[] lbn = { 0, -11, -1, 1 };
		float[] rtf = { 10, 10, -10, 1 };
		Model m = new SimpleModel(tries);

		Rasterizer rast = new Rasterizer(300, 300);

		rast.loadModel(m);
		rast.setShader(new RandomColorShader());
		rast.setProjection(new OrhtogonalProjection(lbn, rtf));//new PerspectiveProjection(lbn, rtf));
		rast.setCameraPosition(new CameraPosition(ar(10, 10, 10, 1),
				ar(0, 1, 0, 1),negate( ar(0, 0, -1, 1))));
		int[][] value = rast.rasterize();
		JFrame frame = new JFrame();
		CgPanel panel = new CgPanel();
		frame.getContentPane().add(panel);
		frame.setBounds(0, 0, 502, 502);
		panel.setBounds(0, 0, 501, 501);
		frame.setVisible(true);
		panel.clear();
		 while (true) {
		 //value = rast.rasterize();
		 panel.setVisible(true);
		 System.out.println("rasterized");
		
		 for (int i = 0; i < value.length; i++) {
		 for (int j = 0; j < value[0].length; j++) {
		 panel.drawPixel(i, j, value[i][j]);
		 }
		 }
		 panel.repaint();
		 }
		// m = new TransformedModel(m,
		// Transformations.transform3DRoationX((float) Math.PI / 10));
	}

	private Collection<Triangle> genTriangles() {
		ArrayList<Triangle> rv = new ArrayList<Triangle>();
		float[][] points = new float[3][4];
		float[][] colors = new float[3][3];
		points[0] = ar(0, 8, 0, 1);
		points[1] = ar(4, 0, 10, 1);
		points[2] = ar(8, 8, 0, 1);
		colors[0] = ar(0, 0, 1);
		colors[1] = ar(0, 0, 1);
		colors[2] = ar(0, 0, 1);
		rv.add(new Triangle(points, colors));
		points = new float[3][4];
		colors = new float[3][3];
		points[0] = ar(8, 0, 0, 1);
		points[1] = ar(4, 4, 10, 1);
		points[2] = ar(0, 0, 0, 1);
		colors[0] = ar(0, 1, 0);
		colors[1] = ar(0, 1, 0);
		colors[2] = ar(0, 1, 0);
		rv.add(new Triangle(points, colors));
		// points = new float[3][4];
		// colors = new float[3][3];
		// points[0] = ar(6,3,2,1);
		// points[1] = ar(6,2,2,1);
		// points[2] = ar(1,3,0,1);
		// colors[0] = ar(1,0,0);
		// colors[1] = ar(1,0,0);
		// colors[2] = ar(1,0,0);
		// rv.add(new Triangle(points, colors));
		return rv;
	}

	public static float[] ar(float... floats) {
		return floats;
	}
}
